<template>
  <div class="WorkingSchedule" v-loading="loading">
    <el-form
      ref="form"
      size="mini"
      :model="form"
      label-position="top"
      class="myform"
    >
      <div>
        <el-form-item label="品名" class="myform-item">
          <el-input
            v-model="form.name"
            placeholder="请输入品名"
            @keyup.enter.native="onSearch"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="SKU" class="myform-item">
          <el-input
            v-model="form.sku_id"
            placeholder="请输入SKU"
            @keyup.enter.native="onSearch"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="成品加工单编码" class="myform-item">
          <el-input
            v-model="form.processing_code"
            placeholder="请输入成品加工单编码"
            @keyup.enter.native="onSearch"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="仓库类型:" class="myform-item">
          <el-select
            v-model="form.wh_id"
            placeholder="请选择"
            clearable
            @keyup.enter.native="onSearch"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="日期" class="myform-item">
          <el-date-picker
            value-format="yyyy-MM-dd"
            v-model="date"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item style="margin-top: 37px">
          <el-button type="primary" @click="onSearch">查询</el-button>
          <el-button type="primary" @click="onExport">导出</el-button>
        </el-form-item>
      </div>
      <div></div>
    </el-form>
    <div style="margin: 10px 0">
      <!-- <el-button type="primary" @click="onUpdate">更新回货单</el-button> -->
      <span>最后更新日期：{{ savetime }}</span>
    </div>
    <el-table
      ref="multipleTable"
      :data="tableData"
      style="width: 100%"
      size="mini"
      :height="browserHeight - 330"
      :summary-method="getSummaries"
      show-summary
      border
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column
        label="入库回货单号"
        align="center"
        prop="io_id"
      ></el-table-column>
      <el-table-column
        label="仓库名称"
        align="center"
        prop="warehouse"
      ></el-table-column>
      <el-table-column
        label="供应商名称"
        align="center"
        prop="supplier_name"
      ></el-table-column>
      <el-table-column
        label="SKU"
        align="center"
        prop="sku_id"
      ></el-table-column>
      <el-table-column
        label="品名"
        align="center"
        prop="name"
      ></el-table-column>
      <el-table-column
        label="加工单号"
        align="center"
        prop="so_id"
      ></el-table-column>
      <el-table-column
        label="采购单号"
        align="center"
        prop="po_id"
      ></el-table-column>
      <el-table-column label="数量" align="center" prop="qty"></el-table-column>
      <el-table-column
        label="含税单价"
        align="center"
        prop="cost_price"
      ></el-table-column>
      <el-table-column
        label="回货日期"
        align="center"
        prop="io_date"
      ></el-table-column>
      <el-table-column
        label="可对账日期"
        align="center"
        prop="ke_date"
      ></el-table-column>
      <el-table-column label="对账状态" align="center" prop="ke_status">
        <template scope="{ row }">
          {{ row.ke_status == 0 ? "不可对账" : "可对账" }}
        </template>
      </el-table-column>
      <el-table-column label="是否对账" align="center" prop="k_status">
        <template scope="{ row }">
          {{ row.k_status == 0 ? "未对账" : "已对账" }}
        </template>
      </el-table-column>
      <el-table-column
        label="货款金额"
        align="center"
        prop="cost_amount"
      ></el-table-column>
      <el-table-column
        label="备注"
        align="center"
        prop="remark"
      ></el-table-column>
    </el-table>
    <div style="margin-top: 20px">
      <el-button type="primary" size="mini" @click="onPurchaseinList(1)"
        >延期1个月</el-button
      >
      <el-button type="primary" size="mini" @click="onPurchaseinList(2)"
        >延期2个月</el-button
      >
      <el-button type="warning" size="mini" @click="onPurchaseinList(3)"
        >寄卖</el-button
      >
      <el-button type="success" size="mini" @click="onPurchaseinList(0)"
        >正常</el-button
      >
    </div>
    <pagination
      :form="form"
      :total="total"
      @paginationPostOrderTaskList="postAdminPricingGetPurchaseinList"
    />
  </div>
</template>

<script>
import {
  postAdminPricingGetPurchaseinList,
  postAdminHomeSsySetJushuitanPurchasein_Query,
  postAdminPricingSetPurchaseinList,
} from "@/api";
import pagination from "@/components/pagination.vue";
export default {
  components: { pagination },
  data() {
    return {
      loading: false,
      savetime: "",
      browserHeight: "",
      options: [
        {
          value: 0,
          label: "全部",
        },
        {
          value: 1,
          label: "主仓",
        },
        {
          value: 2,
          label: "销退仓",
        },
        {
          value: 3,
          label: "进货仓",
        },
        {
          value: 4,
          label: "次品仓",
        },
      ],
      date: "",
      form: {
        processing_code:"",
        sku_id: "",
        name: "",
        wh_id: 3,
        str_time: "",
        end_time: "",
        page: 1,
        size: 10,
      },
      tableData: [],
      total: 1,
      // 审核
      ExtraAudit: {
        id: "",
        status: 1,
      },
      multipleSelection: [],
    };
  },
  activated() {
    this.browserHeight =
      window.innerHeight ||
      document.documentElement.clientHeight ||
      document.body.clientHeight;
    let form = this.form;
    if (this.$route.params.processing_code) {
      this.form.processing_code = this.$route.params.processing_code;
    }

    this.postAdminPricingGetPurchaseinList(form);
  },
  updated() {
    this.$nextTick(() => {
      this.$refs.multipleTable.doLayout();
    });
  },
  methods: {
    onExport(){
      let date = this.date
      if (date) {
        this.form.str_time = date[0];
        this.form.end_time = date[1];
      }else{
        this.form.str_time = "";
        this.form.end_time = "";
      }
      let data = this.form
      window.open(`https://ssy.13yu.com/index.php/Admin/Excel/getPurchaseinList?sku_id=${data.sku_id}&wh_id=${data.wh_id}&str_time=${data.str_time}&end_time=${data.end_time}&name=${data.name}&processing_code=${data.processing_code}`)
    },
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        if (!values.every((value) => isNaN(value)) && column.label == "数量") {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] = sums[index];
        } else {
          sums[index] = "";
        }
      });
      return sums;
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    onPurchaseinList(data) {
      let that = this;
      let { multipleSelection, form } = this;
      let ids = [];
      multipleSelection.forEach((e) => {
        ids.push(e.io_id);
      });
      let obj = {
        type: data,
        ids,
      };
      this.$confirm(
        `确定${
          data == 1
            ? "延期1个月"
            : data == 2
            ? "延期2个月"
            : data == 3
            ? "寄售"
            : data == 0
            ? "正常"
            : ""
        }？`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          postAdminPricingSetPurchaseinList(obj).then((res) => {
            that.postAdminPricingGetPurchaseinList(form);
            this.$message({
              type: "success",
              message: "提交成功!",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消提交",
          });
        });
    },
    // 更新回货单
    onUpdate() {
      this.loading = true;
      let form = this.form;
      postAdminHomeSsySetJushuitanPurchasein_Query().then((res) => {
        this.postAdminPricingGetPurchaseinList(form);
      });
    },
    // 采购列表
    async postAdminPricingGetPurchaseinList(data) {
      this.loading = true;
      this.form = data;
      postAdminPricingGetPurchaseinList(data)
        .then((res) => {
          this.loading = false;
          console.log("采购列表", res);
          this.savetime = res.savetime;
          this.tableData = res.data;
          this.total = Number(res.total);
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    // 搜索
    onSearch() {
      let date = this.date;
      if (date) {
        this.form.str_time = date[0];
        this.form.end_time = date[1];
      }
      this.form.page = 1;
      this.form.size = 10;
      this.postAdminPricingGetPurchaseinList(this.form);
    },
  },
};
</script>

<style lang="scss" scoped>
.myform {
  display: flex;
  justify-content: space-between;
  > div {
    display: flex;
  }
  &-item {
    margin-right: 10px;
  }
}
.WorkingSchedule {
  background: #ffff;
  padding: 20px;
  border-radius: 2px;
  @include boxShadow;
}
</style>
